package com.jmyd.sharding.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.jmyd.sharding.base.dto.KeywordQueryDto;
import com.jmyd.sharding.base.service.impl.BaseServiceImpl;
import com.jmyd.sharding.base.vo.PageBean;
import com.jmyd.sharding.domain.DomainPoFactory;
import com.jmyd.sharding.domain.dto.SysUserDto;
import com.jmyd.sharding.domain.dto.SysUserUpdateDto;
import com.jmyd.sharding.domain.po.SysUser;
import com.jmyd.sharding.domain.vo.SysUserVo;
import com.jmyd.sharding.mapper.SysUserMapper;
import com.jmyd.sharding.service.ISysUserService;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author samphin
 * @date 2024/6/18 10:46
 */
@Service
public class SysUserService extends BaseServiceImpl<Long, SysUserDto, SysUser> implements ISysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 保存数据
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @ShardingTransactionType(TransactionType.XA)
    public boolean save(SysUserDto dto) {
        SysUser sysUser = DomainPoFactory.newInstance(SysUser.class, null).build(dto);
        sysUser.setDeleted(false);
        this.sysUserMapper.insertSelective(sysUser);
        return true;
    }

    @Override
    public boolean updateById(SysUserUpdateDto dto) {
        SysUser sysUser = new SysUser().build(dto);
        this.sysUserMapper.updateById(sysUser);
        return true;
    }

    @Override
    public boolean deleteById(Long id) {
        this.sysUserMapper.deleteById(id);
        return true;
    }

    @Override
    public PageBean<SysUserVo> paging(KeywordQueryDto queryDto) {
        final Page<Object> page = PageMethod.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        final List<SysUserVo> voList = this.sysUserMapper.paging(queryDto);
        return new PageBean<>(voList, page);
    }

    @Override
    public SysUserVo queryDetail(Long id) {
        final SysUser sysUser = this.sysUserMapper.selectById(id);
        return new SysUserVo().buildVo(sysUser);
    }
}